<template>
    <div class="role-wrap" style="padding: 6px">
        <el-card>
            <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
                <!-- 通道号 摄像机安装位置 IP 运维人员 -->
                <el-form-item label="通道号" prop="channelId">
                    <el-input v-model="queryParams.channelId" placeholder="请输入通道号" clearable style="width: 180px" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="摄像机安装位置" prop="installSite">
                    <el-input v-model="queryParams.installSite" placeholder="请输入摄像机安装位置" clearable style="width: 180px" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="IP" prop="ip">
                    <el-input v-model="queryParams.ip" placeholder="请输入IP" clearable style="width: 180px" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="运维人员" prop="opsStaff">
                    <el-input v-model="queryParams.opsStaff" placeholder="请输入运维人员" clearable style="width: 180px" @keyup.enter.native="handleQuery" />
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="main-card">
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
                    <el-button type="danger" size="mini" @click="deleteIds">批量删除</el-button>
                    <el-button type="success" size="mini" @click="batchUpdate">批量修改</el-button>
                    <el-button size="mini" @click="lanquInfo">同步信息</el-button>
                </el-col>
                <right-toolbar @queryTable="getList"></right-toolbar>
            </el-row>
            <el-table ref="multipleTable" class="base-table" v-loading="loading" :data="roleList" @selection-change="handleSelectionChange">
                <el-table-column align="center" type="selection" width="55" />
                <el-table-column label="通道号" prop="channelId" align="left" />
                <el-table-column label="摄像机安装位置" prop="installSite" align="left"  width="130"/>
                <el-table-column label="品牌" prop="brand" align="left" />
                <el-table-column label="用户名" prop="userName" align="left" />
                <el-table-column label="密码" prop="password" align="left" />
                <el-table-column label="IP" prop="ip" align="left" />
                <el-table-column label="RTSP地址" prop="rtsp" align="left" />
                <el-table-column label="HLS地址" prop="hls" align="left" />
                <el-table-column label="摄像机状态" align="status" width="100">
                    <template slot-scope="scope">
                        <div v-if="scope.row.status == 0">离线</div>
                        <div v-else>在线</div>
                    </template>
                </el-table-column>
                <el-table-column label="运维人员" align="center" prop="opsStaff" width="100" />
                <el-table-column label="上次运维时间" align="center" prop="lastOpsTime"></el-table-column>
                <el-table-column label="运维到期时间" align="center" prop="opsExpirationTime" />
                <el-table-column label="离线时间" align="center" prop="offTime" width="100" />
                <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <el-button size="small" type="primary" plain style="padding: 5px" icon="el-icon-edit" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="small" type="danger" plain style="padding: 5px" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
        </el-card>

        <!-- 添加或修改角色配置对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
            <div style="margin-top: -55px">
                <el-divider style="margin-top: -30px"></el-divider>
                <el-form ref="form" :model="form" :rules="rules" label-width="160px">
                    <el-form-item label="通道号" prop="channelId" v-if="isBatch != 3">
                        <el-input v-model="form.channelId" placeholder="请输入通道号" :disabled="isBatch == 2"></el-input>
                    </el-form-item>
                    <el-form-item label="摄像机安装位置" prop="installSite" v-if="isBatch != 3">
                        <el-input v-model="form.installSite" placeholder="请输入摄像机安装位置"></el-input>
                    </el-form-item>
                    <el-form-item label="品牌" prop="brand">
                        <el-input v-model="form.brand" placeholder="请输入品牌"></el-input>
                    </el-form-item>
                    <el-form-item label="用户名" prop="userName" v-if="isBatch != 3">
                        <el-input v-model="form.userName" placeholder="请输入用户名"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="password" v-if="isBatch != 3">
                        <el-input v-model="form.password" placeholder="请输入密码"></el-input>
                    </el-form-item>
                    <el-form-item label="IP" prop="ip" v-if="isBatch != 3">
                        <el-input v-model="form.ip" placeholder="请输入IP"></el-input>
                    </el-form-item>
                    <el-form-item label="RTSP地址" prop="rtsp" v-if="isBatch != 3">
                        <el-input v-model="form.rtsp" placeholder="请输入RTSP地址"></el-input>
                    </el-form-item>
                    <el-form-item label="HLS地址" prop="hls" v-if="isBatch != 3">
                        <el-input v-model="form.hls" placeholder="请输入HLS地址"></el-input>
                    </el-form-item>
                    <el-form-item label="摄像机状态" prop="status" v-if="isBatch != 3">
                        <el-radio-group v-model="form.status">
                            <el-radio label="0">离线</el-radio>
                            <el-radio label="1">在线</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="运维人员" prop="opsStaff">
                        <el-input v-model="form.opsStaff" placeholder="请输入运维人员"></el-input>
                    </el-form-item>
                    <el-form-item label="上次运维时间" prop="lastOpsTime" v-if="isBatch != 3">
                        <el-date-picker v-model="form.lastOpsTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
                    </el-form-item>
                    <el-form-item label="运维到期时间" prop="opsExpirationTime" v-if="isBatch != 3">
                        <el-date-picker v-model="form.opsExpirationTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
                    </el-form-item>
                </el-form>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { getCarList, getWhiteCarList, addCar, editCar, deleteCar, downloadCarTempate } from '@/api/car/car';
import { queryPageCamera, addCamera, updateCamera, deleteCamera, batchDeleteCamera, batchUpdate, synchronize } from './api/sysoperation.js';
import axios from 'axios';
import { getToken } from '@/utils/auth';
import Treeselect from '@riophae/vue-treeselect';
import '@riophae/vue-treeselect/dist/vue-treeselect.css';

export default {
    dicts: ['sys_normal_disable'],
    components: { Treeselect },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 总条数
            total: 0,
            // 角色表格数据
            roleList: [],
            // 弹出层标题
            title: '添加',
            // 是否显示弹出层
            open: false,

            // 查询参数
            queryParams: {
                carCode: '',
                carplate: '',
                pageNum: 1,
                pageSize: 10,
                // recordType: 1,
                channelId: '',
                installSite: '',
                ip: '',
                opsStaff: '',
            },
            // 表单参数
            form: {
                channelId: '',
            },

            // 表单校验
            rules: {
                carCode: [{ required: true, message: '车辆属性不能为空', trigger: 'change' }],
                whitelistIp: [{ required: true, message: '白名单不能为空', trigger: 'blur' }],
                carPlate: [{ required: true, message: '车牌号不能为空', trigger: 'blur' }],
                cmpName: [{ required: true, message: '归属单位/个人名称不能为空', trigger: 'blur' }],
                carType: [{ required: true, message: '车辆类型不能为空', trigger: 'change' }],
                status: [{ required: true, message: '车辆类型不能为空', trigger: 'change' }],
                start: [{ required: true, message: '车辆通行开始时间不能为空', trigger: 'change' }],
                end: [{ required: true, message: '车辆通行结束时间不能为空', trigger: 'change' }],
                //start
            },
            deviceList: [],
            multipleSelection: [],
            isBatch: 1,
        };
    },
    mounted() {
        this.getList();
    },
    methods: {
        /** 查询列表 */
        getList() {
            this.loading = true;
            queryPageCamera(this.queryParams).then((response) => {
                console.log(response);
                try {
                    this.roleList = response.rows;
                    this.total = response.total;
                } catch (error) {}
                this.loading = false;
            });
        },

        lanquInfo() {
            synchronize().then((res) => {
                this.getList()
            });
        },

        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },

        // 表单重置
        reset() {
            this.form = {};
            this.resetForm('form');
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm');
            this.handleQuery();
        },

        /** 新增按钮操作 */
        handleAdd() {
            this.form.channelId = '';
            this.open = true;
            this.reset();
            this.isBatch = 1;
            this.title = '添加';
            this.isEdit = true;
            // this.getWhiteCarListFun();
        },
        /** 批量删除 */
        deleteIds() {
            if (this.multipleSelection.length <= 0) {
                this.$modal.msgWarning('请勾选');
            } else {
                const channelIds = this.multipleSelection.map((item) => item.channelId);
                this.$modal
                    .confirm('是否确认删除?')
                    .then(function () {
                        return batchDeleteCamera(channelIds);
                    })
                    .then(() => {
                        this.getList();
                        this.$modal.msgSuccess('删除成功');
                    })
                    .catch(() => {});
            }
        },
        /** 批量修改 */
        batchUpdate() {
            console.log(this.multipleSelection.length);
            console.log(this.multipleSelection);
            if (this.multipleSelection.length <= 0) {
                this.$modal.msgWarning('请勾选');
            } else {
                this.isBatch = 3;
                this.open = true;
                this.title == '批量编辑';

                return;
                const channelIds = this.multipleSelection.map((item) => item.channelId);
                batchUpdate(this.form).then((response) => {
                    this.$modal.msgSuccess('操作成功');
                    this.open = false;
                    this.getList();
                });
            }
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },

        getWhiteCarListFun() {
            getWhiteCarList().then((res) => {
                if (res.code == 0) {
                    this.deviceList = res.data;
                }
            });
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            console.log(row);
            // this.getWhiteCarListFun();
            row.status = row.status.toString();
            this.reset();
            this.form = row;
            this.isBatch = 2;
            this.open = true;
            this.title = '编辑';
        },

        /** 提交按钮 */
        submitForm: function () {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    // this.form.whitelistIp = this.form.whitelistIp.toString();
                    console.log(this.form);

                    if (this.isBatch == 2) {
                        updateCamera(this.form).then((response) => {
                            this.$modal.msgSuccess('修改成功');
                            this.open = false;
                            this.getList();
                        });
                    } else if (this.isBatch == 3) {
                        let arr = [];
                        for (let g = 0; g < this.multipleSelection.length; g++) {
                            arr.push({ channelId: this.multipleSelection[g].channelId, brand: this.form.brand, opsStaff: this.form.opsStaff });
                        }

                        // const modifiedData = this.multipleSelection.map((item) => ({
                        //     channelId: item.channelId,
                        //     brand: this.form.brand,
                        //     opsStaff: this.form.opsStaff,
                        // }));

                        console.log(arr);

                        batchUpdate(arr).then((response) => {
                            this.$modal.msgSuccess('批量编辑成功');
                            this.open = false;
                            this.getList();
                        });
                    } else if (this.isBatch == 1) {
                        addCamera(this.form).then((response) => {
                            this.$modal.msgSuccess('新增成功');
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 提交按钮（数据权限） */
        submitDataScope: function () {
            if (this.form.roleId != 0) {
                this.form.deptIds = this.getDeptAllCheckedKeys();
                dataScope(this.form).then((response) => {
                    this.$modal.msgSuccess('修改成功');
                    this.openDataScope = false;
                    this.getList();
                });
            }
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            this.$modal
                .confirm('是否确认删除?')
                .then(function () {
                    console.log(row.channelId);

                    return deleteCamera(row.channelId);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess('删除成功');
                })
                .catch(() => {});
        },
    },
};
</script>
<style>
.role-wrap {
    padding: 6px;
}

.top-card-wrap {
    margin-bottom: 5px;
}

.card-wrap {
    padding-bottom: 100px;
}
</style>
